<template>
  <div>
    <el-dialog v-model="dialogVisible" width="80%" align-center :close-on-click-modal="false" :show-close="false"
      :before-close="handleClose" destroy-on-close draggable>
      <template #header="{ close }">
        <div class="dialog-header flex justify-between items-center">
          <div>评审计划{{ ruleForm.id ? '编辑' : '新增' }}</div>
          <el-icon class="el-icon--left cursor-pointer" @click="close">
            <CircleCloseFilled color="#fff" />
          </el-icon>
        </div>
      </template>
      <el-form :model="ruleForm" :rules="rules" label-width="130px" ref="ruleFormRef">
        <!-- 基本信息 -->
        <div class="bg-white mt-[10px]">
          <BaseHead>基本信息</BaseHead>
          <div class="p-[10px]">

            <el-row :gutter="20">

              <el-col :span="24">
                <el-form-item label="评审计划名称" prop="name">
                  <el-input placeholder="请输入评审计划名称" v-model="ruleForm.name" clearable></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="评审方案" prop="reviewSchemeId">
                  <el-select v-model="ruleForm.reviewSchemeId" placeholder="请选择评审方案" clearable>
                    <el-option v-for="item in fatableData" :key="item.id" :label="item.name" :value="item.id" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="开始日期">
                  <el-date-picker v-model="ruleForm.beginDate" type="date" format="YYYY/MM/DD"
                    value-format="YYYY-MM-DD" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="结束日期">
                  <el-date-picker v-model="ruleForm.endDate" type="date" format="YYYY/MM/DD"
                    value-format="YYYY-MM-DD" />
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="评审指南">
                  <FileUpload :uploadUrl="uploadUrl" :limit="1" tip="最多可上传1个附件,每个附件大小不超过20M" v-model="fileList"
                    @change="fileChange">
                  </FileUpload>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="备注">
                  <el-input v-model="ruleForm.note" type="textarea" />
                </el-form-item>
              </el-col>
            </el-row>
          </div>
        </div>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <div class="operate-btns">
            <el-button type="primary" @click="submitForm(ruleFormRef)">保存</el-button>
            <el-button type="primary" @click="dialogVisibleMet">关闭</el-button>
          </div>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import BaseHead from "@/views/project/components/BaseHead.vue";
import { reviewPlanAdd, getreviewPlanUpdate } from "@/api/achievement/cghj"
import { ElMessage, ElMessageBox } from "element-plus";
import FileUpload from "@/views/achievement/components/FileUpload.vue"
const dialogVisible = defineModel();
const props = defineProps({
  fatableData: Array,
})
const ruleForm = reactive({
  name: '',
  reviewSchemeId: '',
  beginDate: '',
  endDate: '',
  fileId: '',
  note: '',
})
const rules = reactive({
  name: [
    { required: true, message: '请输入评审计划名称', trigger: 'blur' },
  ],
  reviewSchemeId: [
    {
      required: true,
      message: '请选择评审方案',
      trigger: 'change',
    },
  ],
})

const ruleFormRef = ref()
const submitForm = async (formEl) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log('submit!')
      let params = {
        ...ruleForm,
      }
      console.log(params, 'params')
      if (params.id) {
        getreviewPlanUpdate(params).then((res) => {
          if (res.code == 200) {
            ElMessage({
              message: res.msg,
              type: 'success',
            })
            dialogVisible.value = false
            ininData()
            emit('callBack', "")
          } else {
            ElMessage({
              message: res.msg,
              type: 'error',
            })
          }
        })
      } else {
        reviewPlanAdd(params).then((res) => {
          if (res.code == 200) {
            ElMessage({
              message: res.msg,
              type: 'success',
            })
            dialogVisible.value = false
            ininData()
            emit('callBack', "")
          } else {
            ElMessage({
              message: res.msg,
              type: 'error',
            })
          }
        })
      }
    } else {
      console.log('error submit!', fields)
    }
  })
}
const emit = defineEmits(['callBack']);
const handleClose = (done) => {
  Object.assign(ruleForm, {
    name: '',
    reviewSchemeId: '',
    beginDate: '',
    endDate: '',
    fileId: '',
    note: '',
  })
  delete ruleForm.id;
  fileList.value = []
  emit('callBack', "")
  done()
}
const ininData = () => {
  Object.assign(ruleForm, {
    name: '',
    reviewSchemeId: '',
    beginDate: '',
    endDate: '',
    fileId: '',
    note: '',
  })
  delete ruleForm.id;
  fileList.value = []
}
const dialogVisibleMet = () => {
  ininData()
  dialogVisible.value = false;
}
const uploadUrl = `/commonFile/uploadFile?modelName=reviewPlanApply`;
const fileList = ref([]);
const getRuleForm = (i) => {
  for (const key in i) {
    ruleForm[key] = i[key];
  }
  if (i.fileList && i.fileList.length > 0) {
    fileList.value = i.fileList.map((item) => {
      return {
        name: item.fileName,
        id: item.id,
      };
    });
  }
  console.log(ruleForm, 'ruleForm')
}


//上传的文件改变
const fileChange = () => {
  const ids = fileList.value.map((item) => {
    const id = item.response ? item.response.data : item.id;
    return id;
  });
  ruleForm.fileId = ids.join(",");
};
defineExpose({
  getRuleForm
})
</script>

<style scoped lang="scss">
@import url("@/views/achievement/index.scss");
</style>
